<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once ('./application/controllers/authenticate.php');
class Test_grid extends Authenticate {
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('category_model');
		$this->load->library('html/control/fx_control');
		$this->load->library('html/control/fx_filter');
		$this->load->library('html/control/fx_sort');
		$this->category_model->addAttributeOrder('id', 'asc');
		$this->load->library('upload');
	}

	protected function _eventLoadLayout() {
		$this->page ['title'] = 'Test Management';
		$this->module ['title'] = 'Test Management';
		$this->module ['grid'] ['records'] = 10;
		$this->module ['grid'] ['page_range'] = 5;
	}
	
	function index() {
		$data = $this->_initGridView ();
		$this->setPageContent ( 'vuonglh/test/grid', $data );
		$this->renderLayout ();
	}
	
	protected function _initGridView() {
		
		// Get Paginator Params
		$page = $this->input->getParam ( 'page' );
		$recordPerPage = $this->input->getParam ( 'recordPerPage' );
		if (! $recordPerPage) {
			$recordPerPage = $this->module ['grid'] ['records'];
		}
		
		// Get Filter Params
		$filters = $this->input->post ( 'testFilter' );
		// Filter
		$this->fx_filter->filter ( $filters, $this->category_model );
		
		// Get Order Params
		$order = $this->input->post ( 'testOrder' );
		// Sort
		$this->fx_sort->sort ( $order, $this->category_model );
		// Get total found records
		$found = $this->category_model->countTotal ();
		
		// Paginate
		$this->load->library ( "tree/paginator/engine/st_paginator_collection" );
		$this->load->library ( "tree/paginator/st_paginator" );
		$this->st_paginator_collection->setModel ( $this->category_model );
		$this->st_paginator->setConfig ( array ('engine' => $this->st_paginator_collection, 'pageRange' => $this->module ['grid'] ['page_range'], 'itemCountPerPage' => $recordPerPage, 'currentPageNumber' => $page ) );
		
		// Paginate Collection
		$collection = $this->st_paginator->getItemsByPage ( $page );
		if (empty ( $collection )) {
			$collection = array ();
		}
		$data = array ();
		$data ['found'] = $found;
		$data ['sorter'] = $this->fx_sort;
		$data ['filters'] = $this->fx_filter;
		$data ['paginator'] = $this->st_paginator;
		$data ['test_ids'] = $this->input->post ( 'test_ids' );
		$data ['test_ids_array'] = explode ( ',', $this->input->post ( 'test_ids' ) );
		$data ['collection'] = $collection;
		return $data;
		
	}
	
	public function massActive()
	{
		try {
			// Load User Model
			$ids = $this->input->getMassValues('test_ids');
			if ($ids) {
				$this->category_model->query("update catelog_catagory set is_active = 1 where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/vuonglh/test_grid/message', "Active $countItem items successfully");
			redirect(site_url("admin/vuonglh/test_grid/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/vuonglh/test_grid/message', $e->getMessage());
			redirect(site_url("admin/vuonglh/test_grid/index", true));
		}
	}
	
	public function massDeactive()
	{
		try {
			// Load User Model
			$ids = $this->input->getMassValues('test_ids');
			if ($ids) {
				$this->category_model->query("update catelog_catagory set is_active = 0 where id in ($ids)");
			}
			$countItem = count(explode(',', $ids));
			$this->st_registry->register('admin/vuonglh/test_grid/message', "Deactive $countItem items successfully");
			redirect(site_url("admin/vuonglh/test_grid/index", true));
		} catch (Exception $ex) {
			$this->st_registry->register('admin/vuonglh/test_grid/message', $e->getMessage());
			redirect(site_url("admin/vuonglh/test_grid/index", true));
		}
	}
	 
	public function save() 
	{
		echo 'this is save page';
	}
	
	public function delete()
	{
		echo 'this is delete page';
	}

        public function edit(){
            $id = $this->input->getParam('id');
            echo 'this is edit page with id = ' + $id;

        }
}